package cn.xyf.mzmy.server.controller.file;


import cn.xyf.mzmy.common.pojo.Result;
import cn.xyf.mzmy.server.service.file.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@Slf4j
@Tag(name = "文件上传")
public class FileController {

    @Resource(name = "tencentCosFileServiceImpl")
    private FileService fileService;

    @Operation(summary = "文件上传接口")
    @PostMapping("/upload")
    public Result<String> upload(@RequestPart MultipartFile file){
        try {
            // 上传并返回访问地址
            String url = fileService.uploadFile(file);
            return Result.ok(url);
        }
        catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.fail("上传文件失败");
        }
    }

}
